<?php
namespace App\Api\Controllers\Config;


use App\Models\Store;
use App\Models\StorePayWay;
use App\Models\UserRate;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PayWaysController
{
    protected $_data;
    public function __construct($data = [])
    {
        $this->_data = $data;
    }

    //支付用到 分店没有直接公用总店的 通过ways_source查询
    public function ways_source($ways_source, $store_id, $srore_id_pid)
    {
        $ways = StorePayWay::where('ways_source', $ways_source)
            ->where('store_id', $store_id)
            ->where('status', 1)
            ->orderBy('sort', 'asc')
            ->first();
        if (!$ways) {
            $store_id = "";
            $srore_id_pid = Store::where('id', $srore_id_pid)
                ->select('store_id')
                ->first();
            if ($srore_id_pid) {
                $store_id = $srore_id_pid->store_id;
            }

            $ways = StorePayWay::where('ways_source', $ways_source)
                ->where('store_id', $store_id)
                ->where('status', 1)
                ->orderBy('sort', 'asc')
                ->first();
        }

//        return $ways;
        return $this->switchCompany($store_id, $srore_id_pid, $ways);
    }


    //支付用到 分店没有直接公用总店的 通过ways_source查询 只返回银联扫码
    public function ways_source_un_qr($ways_source, $store_id, $srore_id_pid)
    {
        $ways = StorePayWay::where('ways_source', $ways_source)
            ->where('store_id', $store_id)
            ->whereIn('ways_type',[50003,6004, 8004, 13004, 15004, 17004, 19004, 20204, 21003, 26004, 27004,27104, 28004,30052,30013,20803,37003,30022,38003,20004,39003,61004,70003,80003,90003, 100003,140003,120003])
            ->where('status', 1)
            ->orderBy('sort', 'asc')
            ->first();
        if (!$ways) {
            $store_id = "";
            $srore_id_pid = Store::where('id', $srore_id_pid)
                ->select('store_id')->first();
            if ($srore_id_pid) {
                $store_id = $srore_id_pid->store_id;
            }

            $ways = StorePayWay::where('ways_source', $ways_source)
                ->where('store_id', $store_id)
                ->whereIn('ways_type',[50003,6004, 8004, 13004, 15004, 17004, 19004, 20204, 21004, 26004, 27004,27104, 28004,30052,30013,20803,37003,30022,38003,20004,39003, 61004,70003,80003,90003, 100003,140003,120003])
                ->where('status', 1)
                ->orderBy('sort', 'asc')
                ->first();
        }
//        return $ways;
        return $this->switchCompany($store_id, $srore_id_pid, $ways);
    }


    //支付用到 分店没有直接公用总店的 通过ways_type查询
    public function ways_type($ways_type, $store_id, $srore_id_pid)
    {
        $ways = StorePayWay::where('ways_type', $ways_type)
            ->where('store_id', $store_id)
            ->where('status', 1)
            ->first();
        if (!$ways) {
            $store_id = "";
            $srore_id_pid = Store::where('id', $srore_id_pid)
                ->select('store_id')
                ->first();
            if ($srore_id_pid) {
                $store_id = $srore_id_pid->store_id;
            }

            $ways = StorePayWay::where('ways_type', $ways_type)
                ->where('store_id', $store_id)
                ->where('status', 1)
                ->first();
        }
//        return $ways;
        return $this->switchCompany($store_id, $srore_id_pid, $ways);
    }


    //支付用到 分店没有直接公用总店的 通过company,ways_source查询
    public function company_ways_source($company, $ways_source, $store_id, $srore_id_pid)
    {
        $ways = StorePayWay::where('ways_source', $ways_source)
            ->where('company', $company)
            ->where('status', 1)
            ->where('store_id', $store_id)
            ->first();
        if (!$ways) {
            $store_id = "";
            $srore_id_pid = Store::where('id', $srore_id_pid)
                ->select('store_id')
                ->first();
            if ($srore_id_pid) {
                $store_id = $srore_id_pid->store_id;
            }

            $ways = StorePayWay::where('ways_source', $ways_source)
                ->where('company', $company)
                ->where('status', 1)
                ->where('store_id', $store_id)
                ->first();
        }

//        return $ways;
        return $this->switchCompany($store_id, $srore_id_pid, $ways);
    }

    public function switchCompany ($store_id, $srore_id_pid, $ways)
    {
        try {
            $total_amount = 0;
            if (isset($this->_data['total_amount']) && !empty($this->_data['total_amount'])) {
                $total_amount = $this->_data['total_amount'];
            }
            return (new \App\Api\Controllers\User\CompanyController())->autoCompany($store_id, $srore_id_pid, $ways, $total_amount);
            error_log(date('Y-m-d H:i:s')." 【轮询通道-切换通道-返回】： ".json_encode($ways)."\n\n", 3, "/tmp/". 'autoCompany'.date('Ymd').".log");
        } catch (\Exception $e) {
            error_log(date('Y-m-d H:i:s')." 【轮询通道-切换通道-异常】： ".json_encode($e->getMessage())."\n\n", 3, "/tmp/". 'autoCompany'.date('Ymd').".log");
        }
    }


    //支付用到 得到成本费率  通过ways_type查询
    public function getCostRate($ways_type, $user_id)
    {
        $user_rate_obj = UserRate::where('ways_type', $ways_type)
            ->where('user_id', $user_id)
            ->where('status', 1)
            ->orderby('created_at', 'desc')
            ->first();

        return $user_rate_obj;
    }


}
